﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace PC_Asset_Geo_Tagging
{
    class RILHelper
    {
        private static string celltowerinfo = "";
        public static RILCELLTOWERINFO rilCellTowerInfo;

        public static string GetCellTowerInfo()
        {
            IntPtr hRil = IntPtr.Zero;
            IntPtr hRes = IntPtr.Zero;

            celltowerinfo = "";

            hRes = RIL_Initialize(1,                                        // RIL port 1
                                  new RILRESULTCALLBACK(rilResultCallback), // 返回调用结果的回调函数
                                  null,  0, 0,                                      
                                  out hRil);                                //返回RIL实例的handle 

            if (hRes != IntPtr.Zero)
            {
                return "Failed to initialize RIL";
            }
            hRes = RIL_GetCellTowerInfo(hRil);
            waithandle.WaitOne();

            RIL_Deinitialize(hRil);
            return celltowerinfo;
        }

        private static AutoResetEvent waithandle = new AutoResetEvent(false);

        public static void rilResultCallback(uint dwCode,
                                             IntPtr hrCmdID,
                                             IntPtr lpData,
                                             uint cbData,
                                             uint dwParam)
        {
            //构造一个RILCELLTOWERINFO类用于存放数据
             rilCellTowerInfo = new RILCELLTOWERINFO();
            Marshal.PtrToStructure(lpData, rilCellTowerInfo);

            celltowerinfo = rilCellTowerInfo.dwCellID + "-" +
                            rilCellTowerInfo.dwLocationAreaCode + "-" +
                            rilCellTowerInfo.dwMobileCountryCode + "-" +
                            rilCellTowerInfo.dwMobileNetworkCode;

            //回调通知
            waithandle.Set();
        }


        /*  
         * http://msdn2.microsoft.com/en-us/library/aa920069.aspx
         */
        public delegate void RILRESULTCALLBACK(uint dwCode,
                                               IntPtr hrCmdID,
                                               IntPtr lpData,
                                               uint cbData,
                                               uint dwParam);


        /*
         * http://msdn2.microsoft.com/en-us/library/aa922465.aspx
         */
        public delegate void RILNOTIFYCALLBACK(uint dwCode,
                                               IntPtr lpData,
                                               uint cbData,
                                               uint dwParam);

        /*
        * http://msdn2.microsoft.com/en-us/library/aa921533.aspx
        */
        public class RILCELLTOWERINFO
        {
            public uint cbSize;
            public uint dwParams;
            public uint dwMobileCountryCode = 460;
            public uint dwMobileNetworkCode;
            public uint dwLocationAreaCode;
            public uint dwCellID;
            public uint dwBaseStationID;
            public uint dwBroadcastControlChannel;
            public uint dwRxLevel;
            public uint dwRxLevelFull;
            public uint dwRxLevelSub;
            public uint dwRxQuality;
            public uint dwRxQualityFull;
            public uint dwRxQualitySub;
            public uint dwIdleTimeSlot;
            public uint dwTimingAdvance;
            public uint dwGPRSCellID;
            public uint dwGPRSBaseStationID;
            public uint dwNumBCCH;
        }


        #region Invoked Native Functions

        [DllImport("ril.dll")]
        private static extern IntPtr RIL_Initialize(uint dwIndex,
                                                    RILRESULTCALLBACK pfnResult,
                                                    RILNOTIFYCALLBACK pfnNotify,
                                                    uint dwNotificationClasses,
                                                    uint dwParam,
                                                    out IntPtr lphRil);

        [DllImport("ril.dll")]
        private static extern IntPtr RIL_GetCellTowerInfo(IntPtr hRil);

        [DllImport("ril.dll")]
        private static extern IntPtr RIL_Deinitialize(IntPtr hRil);

        #endregion
    }

}

